require(["common", "jQuery", "coAjax", "queryString", "hash_routie", "href"], function(jSouper, $, coAjax, QueryString, hash_routie, href) {
	// 载入商品信息
	hash_routie.on("goodsupdate", function() {
		App.set("title", "商品修改");
		var goods_to_update = App.get("$Cache.goods_to_update");
		if (goods_to_update) {
			_init_goods_to_update(goods_to_update)
		} else {
			queryString = new QueryString(location.hash);
			var model_id = queryString.get("id");
			coAjax.get(appConfig.goods.detail + model_id, function(result) {
				_init_goods_to_update(result.result);
			});
		}
	});
	App.set("$Event.tag.add_new_tag", function() {
		var new_tag_name = $.trim(App.get("$Cache.tag.new_tag_name"));
		if (new_tag_name) {
			coAjax.post(appConfig.bus.add_tag, {
				tag_name: new_tag_name
			}, function(result) {
				result.result._checked = true;
				var bus_tags = App.get("$Cache.shelves.bus_tags") || [];
				bus_tags.unshift(result.result);
				App.set("$Cache.shelves.bus_tags", bus_tags);
			}, function(errorCode, xhr, errorMsg) {
				alert("error", errorMsg)
			});
		}
		App.set("$Cache.tag.new_tag_name", "");
	});
	App.set("$Event.tag.use_tag", function(e, vm, cvm) {
		cvm.set("_checked", true);
	});
	App.set("$Event.tag.unuse_tag", function(e, vm) {
		vm.set("_checked", false);
		App.set('isSelected', false);
		App.set('isSelected', true);
	});

	//正式上传
	function _upload_goods() {
		var new_goods_info = App.get("$Cache.new_goods");
		var tags = [];
		//提取选中的tags
		var bus_tags = App.get("$Cache.shelves.bus_tags") || [];
		bus_tags.forEach(function(tag) {
			if (tag._checked) {
				tags.push(tag._id);
			}
		});
		new_goods_info.tags = tags;
		//提取有效的详情图
		var _detail_img_urls = App.get("$Cache.shelves.detail_img_urls");
		var detail_img_urls = [];
		_detail_img_urls.forEach(function(detail_img) {
			if (detail_img && detail_img.url) {
				detail_img_urls.push(detail_img.url);
			}
		});
		new_goods_info.detail_img_urls = detail_img_urls;

		debugger
		coAjax.put(appConfig.bus.put_goods, new_goods_info, function(result) {
			_unlock_post();
			// App.set("$Cache.shelves.upload_info","商品添加成功");
			alert("success", "商品修改成功")
				//解锁提交按钮，清空表单
			_init_goods_to_update(result.result)
			_unlock_post();
			href.jump("mobile.admin.html#default/goodsmanage?cid=1");

		}, function(errorCode, xhr, errorMsg) {
			_unlock_post();
			switch (errorCode) {
				case "00068":
					alert("error", "还未输入“标题”");
					break;
				case "00012":
					alert("error", "请先填写表单");
					break;
			}
			alert("error", errorMsg)
			_unlock_post();
		});
	};
	App.set("$Event.shelves.put_goods", function(e) {
		//锁定提交按钮，避免再次提交
		_lock_post();
		var preview_img_url = App.get("$Cache.new_goods.preview_img_url")
			//校验相关数据
		if (!preview_img_url) {
			//如果在上传中，等待上传完成
			if (App.get("$Cache.shelves.preview_img_url_upload_status")) {
				// var _wait = setInterval(function () {
				// 	if (!App.get("$Cache.shelves.preview_img_url_upload_status")) {
				// 		clearInterval(_wait);
				// 		_upload_goods();
				// 	}
				// },200);
				alert("warn", "请等待商品封面图片上传完成");
				_unlock_post();
			} else {
				alert("warn", "请上传商品封面图片");
				_unlock_post();
			}
			return;
		}
		var _detail_img_urls = App.get("$Cache.shelves.detail_img_urls");
		var _warn_str;
		_detail_img_urls.forEach(function(detail_img) {
			if (detail_img.status) {
				_warn_str = "请等待商品详情图上传完成";
			}
		});
		if (_warn_str) {
			alert("warn", _warn_str);
			_unlock_post();
			return;
		}

		//检查通过，上传商品
		_upload_goods();
	});
	//修改商品
	function _lock_post() {
		App.set("$Cache.shelves.upload_info", "商品修改中");
		App.set("$Cache.shelves.lock_submit_button", true);
	}

	function _unlock_post() {
		App.set("$Cache.shelves.upload_info", "");
		App.set("$Cache.shelves.lock_submit_button", false);
	}



	function _init_goods_tags(goods_to_update, bus_tags) {
		var tags_map = {};
		var goods_tags = goods_to_update.tags || [];
		for (var i = 0, tag; tag = goods_tags[i]; i += 1) {
			tags_map[tag] = true;
		}
		for (var i = 0, tag; tag = bus_tags[i]; i += 1) {
			if (tags_map[tag._id]) {
				tag._checked = true;
			}
		}
		App.set("$Cache.shelves.bus_tags", bus_tags);
	}

	function _init_detail_imgs(goods_to_update) {
		var detail_img_urls = goods_to_update.detail_img_urls;
		var _detail_img_urls = [];
		_detail_img_urls.length = appConfig.server_config.storeImagesNumber;
		for (var i = 0, len = detail_img_urls.length, url; url = detail_img_urls[i]; i += 1) {
			_detail_img_urls[i] = {
				url: url
			}
		}
		App.set("$Cache.shelves.detail_img_urls", _detail_img_urls)
	}

	function _init_goods_to_update(goods_to_update) {
		App.set("$Cache.new_goods", goods_to_update);
		//反向绑定标签
		coAjax.get(appConfig.bus.tags, {
			bus_id: busInfo._id
		}, function(result) {
			_init_goods_tags(goods_to_update, result.result);
		});
		//反向绑定详情图片
		_init_detail_imgs(goods_to_update);
	}
});